const fetch = require("../../module/service.js") //必须使用相对路径
const get_from = require("../../module/service.js").get_from;
Page({
  data:{
    shop_id: 0 ,
    user_id: 0,
    detaillist:[],
    detailgoods:0,
    remarks: '',
    detailmessage:{},
    rating: 5.0
  },
  onLoad(options){
    //获取组件的数据
    // let shopInfo = getApp().globalData.shopInfo;
    // this.setData({
    //   // detaillist:shopInfo.curr_translist,
    //   detailgoods:shopInfo.curr_isgoods,
    //   remarks:options.remarks   //这是备注的信息
    // });

    this.data.shop_id = getApp().globalData.curr_shop_id
    //展示加载图
    wx.showLoading({
      title: '加载中...',
      mask:true
    });
    setTimeout(()=>{
      wx.hideLoading();
    }, 2000);
    
    //得到服务器的数据
    //获取用户订单
    let user_id = getApp().globalData.user_id;
    let order_num = options.order_num;
    if (!user_id)
      user_id=0;
    this.data.user_id = user_id;
    if (!order_num)
      order_num=0;
    console.log("order_num", order_num);
    
    get_from('user/order', {user_id: user_id, order_num: order_num}).then((res)=>{
      console.log(res.data);
      this.setData({
        detailmessage: res.data,
        detaillist: res.data.shop_list,
        detailgoods: res.data.total,
        remarks: res.data.remark_message
      });
      console.log(this.data.detailmessage);
      //去掉加载图
      wx.hideLoading();
    }, ()=>{
      wx.showToast({
        title: '数据请求错误',
        icon:'error',
        duration:500
      });
      wx.hideLoading();
    });
  },

  //使用页面回退的方式进入到订单列表
  // onUnload(){ 
  //   wx.switchTab({
  //     url: '/pages/list/list'
  //   });
  // }

  getInput(e) {
    let input = e.detail.value;
    if (input == '')
      input = 5.0
    // console.log(input);
    if (input) {
      this.data.rating = input;
    }
  },

  rating_sure(e){
    let food_id = parseInt(e.currentTarget.dataset.item);
    console.log(e.currentTarget)
    let rating = this.data.rating;
    fetch.post_to('user/updateRating', {user_id: this.data.user_id, food_id: food_id, rating: rating, order_id: this.data.detailmessage.order_id}).then((res)=>{
      if (res.data.msg == 'success')
        wx.showToast({
          title: '评分成功',
          icon: 'success'
        });
    });
  },
});